import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../common/http/DioMgr.dart';
import '../../common/models/product_model_entity.dart';


class baseUI extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("UI组件与布局",textAlign: TextAlign.center,),
        leading: Builder(builder: (context) {
          return IconButton(icon: Icon(Icons.menu,color: Colors.white,), onPressed: ()=>{
            Scaffold.of(context).openDrawer()
          });
        }),
        centerTitle: true,

      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FlatButton(onPressed:()=> _pushNewRoute(context), child: Text("Push VC")),
            FlatButton(onPressed:()=> _WidgetStateMgrAndBaseWidget(context), child: Text("WidgetStateMgr And Base Widget")),
            FlatButton(onPressed:()=> _Layout(context), child: Text("Layout")),
            FlatButton(onPressed: ()=> {Navigator.pushNamed(context, "ContainerVC")}, child: Text("Container Widget")),
            FlatButton(onPressed: ()=> {Navigator.pushNamed(context, "ScrollViewListVC")}, child: Text("ScrollView")),
            FlatButton(onPressed: _appleLogin, child: Text("ApleLogin")),
            FlatButton(onPressed: _httpGet, child: Text("HTPP GET")),
          ],
        ),
      ),
    );
  }



  void _httpGet(){
    DioManager.getInstance().get('/products/deals', {},(data){
      // debugPrint("HTTP-RES:"+data[0].toString());

      List res = data as List;
      List<ProductModelEntity> arr = List();
      debugPrint("===== count:"+res.length.toString());
      for (int i = 0;i < res.length; i++) {
        var m = ProductModelEntity().fromJson(data[i]);
        arr.add(m);
        debugPrint("===== name:${m.name} id:${m.id}");
      }

      // debugPrint("===== ProductModelEntity.name:${m}");
    } , (error){
      debugPrint("HTTP-RES-ERROR:");
    });
  }

  Future<void> _appleLogin() async {
    debugPrint("_appleLogin");
    const platform = MethodChannel("login");
    debugPrint("platform:$platform");
    Map<dynamic,dynamic> result;

    // Map<String, dynamic> params = new Map<String, dynamic>();

    try {
      result = await platform.invokeMethod("login_apple", []);
      debugPrint("result:"+result.cast().toString());
      // params = result as Map<String, dynamic>;


    }catch(e){
      debugPrint("苹果登陆失败！$e");
    }

    // if (params == null){
    //   debugPrint("苹果登陆获取OpenId失败");
    //   return;
    // }
    //
    // final openId = params["openId"];
    //
    // if (openId.isEmpty){
    //   debugPrint("苹果登陆获取OpenId为空！");
    //   return;
    // }
    debugPrint("苹果登陆开始请求数据！");
  }

  void _Layout(context){
    Navigator.pushNamed(context, "LayoutListVC");
  }

  void _WidgetStateMgrAndBaseWidget(context){
    Navigator.pushNamed(context, "widget_state");
  }

  void _pushNewRoute(context){
    Navigator.pushNamed(context, "login",arguments: "已登录");
  }
}